package com.jk.hly.aspect;


import com.jk.hly.bean.HlyUser;
import com.jk.hly.bean.LoginLog;
import lombok.val;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Aspect
@Component
public class LoginAspect {
    @Autowired
    MongoTemplate mongoTemplate;

    @Pointcut("execution(* com.jk.hly.controller.HlyLoginController.userPhoneLogin(..))")
    public void pointCut(){
    }
    @AfterReturning(value = "pointCut()")
    public void AfterLogin(JoinPoint call){

        HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession  session = request.getSession();
        String str = (String) session.getAttribute("str");
        String phone = (String) session.getAttribute("phone");
        LoginLog loginLog=null;
        if(str.equals("1")){
            loginLog=new LoginLog(phone,"登录成功",new Date());
        }if(str.equals("2")){
            loginLog=new LoginLog(phone,"登录失败!该手机号未绑定!",new Date());
        }if(str.equals("3")){
            loginLog=new LoginLog(phone,"登录失败!验证码输入有误!",new Date());
        }if(str.equals("4")){
            loginLog=new LoginLog(phone,"登录失败!该手机号已入黑名单!",new Date());
        }
        mongoTemplate.save(loginLog);
    }
}
